--衍生技 殊路同归
local shulutonggui = fk.CreateSkill {
    name = "mcdh_shulutonggui",
}
local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

shulutonggui:addEffect(fk.EventPhaseEnd,{
    can_trigger = function(self, event, target, player, data)
        local room =player.room
        if target == player  and player:hasSkill(self) then
            if player.phase == Player.Play  and event == fk.EventPhaseEnd and not player:isKongcheng() and table.find(player.room.alive_players, function (p) return player:canPindian(p) end) then
                local types = {}
                room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                    local use = e.data[1]
                    if use and use.from == target.id then
                        table.insertIfNeed(types, use.card.type)
                    end return false
                end, Player.HistoryPhase)
                return #types == 1
            else
                return player.phase == Player.Discard and player:getMark("mcdh_ammo_max") > 0 and event == fk.EventPhaseStart
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room =player.room
        if player.phase == Player.Play then
            local targets = table.filter(player.room.alive_players, function (p) return player:canPindian(p) end)
            local tos = room:askToChoosePlayers(player,{targets= targets,min_num=1,max_num=1,skill_name=self.name,prompt="#shulutonggui-choose"})
            if #tos > 0 then
                self.cost_data = tos[1]
                return true
            end
        else
            local choices = {}
            for i = 1, player:getMark("mcdh_ammo_max"), 1 do
                table.insert(choices, tostring(i))
            end
            table.insertIfNeed(choices,"cancel")
            local choice = room:askForChoice(target, choices, self.name)
            if choice ~= "cancel" then
                self.cost_data = tonumber(choice)
                return true
            end
        end


    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if player.phase == Player.Play then
            local to = self.cost_data
            local pindian = player:pindian({to}, self.name)
            local winner = pindian.results[to.id].winner
            if winner == player  then
                local choices = {"shulutonggui1","shulutonggui2"}
                if  to:isNude() then
                    table.removeOne(choices,"shulutonggui2" )
                end
                local choice = room:askForChoice(player, choices, self.name, nil, nil,{"shulutonggui1","shulutonggui2"})
                if choice == "shulutonggui1" then
                    player:drawCards(2,self.name)
                else
                    player:drawCards(1,self.name)
                    room:askForDiscard(to, 1, 1, true, self.name, false, ".", "#shulutonggui-discard")
                end
            end
        else
            lib.mcdh_ammo(player, 0, -self.cost_data)
            room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, self.cost_data)
        end


    end
})

shulutonggui:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        local room =player.room
        if target == player  and player:hasSkill(self) then
            if player.phase == Player.Play  and event == fk.EventPhaseEnd and not player:isKongcheng() and table.find(player.room.alive_players, function (p) return player:canPindian(p) end) then
                local types = {}
                room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                    local use = e.data[1]
                    if use and use.from == target.id then
                        table.insertIfNeed(types, use.card.type)
                    end return false
                end, Player.HistoryPhase)
                return #types == 1
            else
                return player.phase == Player.Discard and player:getMark("mcdh_ammo_max") > 0 and event == fk.EventPhaseStart
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room =player.room
        if player.phase == Player.Play then
            local targets = table.filter(player.room.alive_players, function (p) return player:canPindian(p) end)
            local tos = room:askToChoosePlayers(player,{targets= targets,min_num=1,max_num=1,skill_name=self.name,prompt="#shulutonggui-choose"})
            if #tos > 0 then
                self.cost_data = tos[1]
                return true
            end
        else
            local choices = {}
            for i = 1, player:getMark("mcdh_ammo_max"), 1 do
                table.insert(choices, tostring(i))
            end
            table.insertIfNeed(choices,"cancel")
            local choice = room:askForChoice(target, choices, self.name)
            if choice ~= "cancel" then
                self.cost_data = tonumber(choice)
                return true
            end
        end


    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if player.phase == Player.Play then
            local to = self.cost_data
            local pindian = player:pindian({to}, self.name)
            local winner = pindian.results[to.id].winner
            if winner == player  then
                local choices = {"shulutonggui1","shulutonggui2"}
                if  to:isNude() then
                    table.removeOne(choices,"shulutonggui2" )
                end
                local choice = room:askForChoice(player, choices, self.name, nil, nil,{"shulutonggui1","shulutonggui2"})
                if choice == "shulutonggui1" then
                    player:drawCards(2,self.name)
                else
                    player:drawCards(1,self.name)
                    room:askForDiscard(to, 1, 1, true, self.name, false, ".", "#shulutonggui-discard")
                end
            end
        else
            lib.mcdh_ammo(player, 0, -self.cost_data)
            room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, self.cost_data)
        end


    end

})

Fk:loadTranslationTable{
    ["mcdh_shulutonggui"] = "殊路同归",
    [":mcdh_shulutonggui"] = "出牌阶段结束时,若你本阶段只使用过一种类型的牌,你可以与一名角色拼点,若你赢,你选择一项:1.摸两张牌;2.摸一张牌并令其弃置一张牌。弃牌阶段开始时,你可以减任意点弹药上限,令本回合你手牌上限增加等量值。",
    ["#shulutonggui-choose"] = "殊路同归:你可以与一名角色拼点",
    ["shulutonggui1"] = "1.摸两张牌",
    ["shulutonggui2"]  ="2.摸一张牌并令其弃置一张牌"
}

return shulutonggui